<template>
  <div id="Header">
    <!-- 头部 -->
    <div class="header_content">
      <div class="container">
        <div class="container_left">
          <div class="left_img">
          </div>
        </div>
      </div>
    </div>
    <!-- 内容主体 -->
    <div class="content">
      <div class="content_bgc">
        <div class="content_login">
          <!-- 登陆左侧 -->
          <div class="login_left">
            <div class="login_left_bgc">
              <div class="login_welcome">
                <img src="@/assets/images/Home/login_left.png" alt="">
              </div>
            </div>
          </div>
          <!-- 登陆右侧 -->
          <div class="login_right">
            <div class="login_bg">
              <div class="login_view">
                <div class="login_header">
                  <div class="view_img">
                    <img src="/logo1.png" alt="">
                  </div>
                  <div class="view_x">|</div>
                  <div class="view_text">登录</div>
                </div>
                <div class="view_input">
                  <el-form ref="login_data_ref" :model="login_data" :rules="login_data_rules">
                    <el-form-item prop="phone">
                      <div class="input_value">
                        <el-input class="inp" type="text" size="large" placeholder="手机号码" v-model="login_data.phone"
                          @keyup.enter="handleLogin" />
                      </div>
                    </el-form-item>
                    <el-form-item prop="password">
                      <div class="input_value">
                        <el-input class="inp" type="password" placeholder="密码" size="large"
                          v-model="login_data.password" @keyup.enter="handleLogin" />
                      </div>
                    </el-form-item>
                    <el-form-item prop="code">
                      <div class="input_value">
                        <el-input class="inp inp_code" type="text" placeholder="验证码" size="large"
                          v-model="login_data.code" @keyup.enter="handleLogin">
                          <template #append>
                            <el-image @click="update_validate_image" class="code-image" :src="validate_image_url">
                              <template #error>
                                <el-text type="info" size="small" @click="update_validate_image">点击刷新</el-text>
                              </template></el-image>
                          </template>
                        </el-input>

                      </div>
                    </el-form-item>
                    <div class="button-view">
                      <el-button color="#6550f5" class="login_btn" size="large" :loading="login_loading"
                        @click="handleLogin">登录</el-button>

                      <!-- <el-link class="tec_login_btn" :loading="login_loading" @click="handleJumpTecLogin">组委会</el-link> -->
                      <el-link class="password_btn" :loading="login_loading"
                        @click="handleForgetPassword">忘记密码</el-link>

                    </div>
                  </el-form>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { useUserStore } from "@/store/user";
import { ElMessage } from "element-plus";
import { useValidateImage } from "@/hooks/useValidateImage";
import { useRouter } from "vue-router";
import { checkPhone } from '@/utils/check.js'
const router = useRouter()
// 使用自定义 Hook 获取验证图片相关数据
const { validate_image_url, update_validate_image } = useValidateImage();

// 使用用户 Store
const userStore = useUserStore();

// 创建 ref 来存储登录数据
const login_data_ref = ref();

// 创建 ref 来存储登录表单验证规则
const login_data_rules = ref({
  phone: [
    {
      required: true,
      validator: checkPhone,
      trigger: "blur",
    },
  ],
  password: [
    {
      type: "string",
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
  ],
  code: [
    {
      type: "string",
      required: true,
      message: "请输入验证码",
      trigger: "blur",
    },
  ],
});
/**
 * 登录数据
 */
const login_data = ref({
  phone: "",
  password: "",
  code: "",
});

/**
 * 登录状态
 */
const login_loading = ref(false);

/**
 * 登录处理函数
 */
const handleLogin = () => {
  login_data_ref.value.validate(async (valid, fields) => {
    if (!valid) {
      return;
    }
    login_loading.value = true;
    let res = await userStore.handleLogin(login_data.value);
    if (!res.success) {
      // 登录失败处理
      ElMessage.error(res.message);
      // 更新验证码图片
      update_validate_image();
    } else {
      // 登录成功处理
      ElMessage.success(res.message);
      // 跳转到管理页面
      router.push({
        name: "管理页",
      });
    }
    login_loading.value = false;
  });
};

/**
 * 忘记密码处理函数
 */
const handleForgetPassword = () => {
  router.push({
    name: "forgetPassword",
  })
};


</script>

<style lang="scss" scoped>
#Header {
  height: 100vh;
  background: #8473f724;

  // 头部主体
  .header_content {
    display: none !important;
    width: 100%;
    height: 100px;
    font-size: 18px;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    // background: #312366;
    color: #333;
    line-height: 60px;

    .container {
      width: 1200px;
      margin: 0 auto;

      .container_left {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 1180px;
        min-height: 1px;
        margin-right: 10px;
        margin-left: 10px;

        .left_img {
          height: 50px;

          img {
            height: 100%;
          }
        }
      }
    }
  }

  // 内容主体
  .content {
    .content_bgc {
      height: 100vh;
      width: 100%;
      background-repeat: no-repeat;
      background-size: 100%;
      background: url("@/assets/images/login_bgc.png");
      // margin-bottom: 350px;

      .content_login {
        display: flex;
        border-radius: 12px;
        background: #fff;
        margin: 0 auto;
        box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.05);
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        // margin-top: 100px;

        // 左侧
        .login_left {
          display: flex;
          align-items: center;
          justify-content: center;

          .login_left_bgc {
            height: 100%;
          }

          .login_welcome {
            display: flex;
            width: 344px;
            height: 100%;
            align-items: center;
            justify-content: center;

            img {
              width: 100%;
              height: 100%;
              border-radius: 12px 0 0 12px;
            }

            .login_btn {
              position: absolute;
              //   bottom: 40px;
              left: 50%;
              transform: translateX(-50%);
              width: 132px;
            }
          }
        }

        // 右侧
        .login_right {
          width: 370px;
          height: 100%;
          display: flex;
          display: flex;
          align-items: center;
          justify-content: center;

          .login_bg {
            border-radius: 5px;
            width: 335px;
            height: 415px;

            .login_view {
              width: 335px;
              height: 290px;
              margin-top: 64px;
              margin-bottom: 24px;
              text-align: center;

              .login_header {
                display: flex;
                align-items: center;
                justify-content: center;
                height: 24px;
                line-height: 24px;

                .view_img {
                  width: 80px;
                  height: 24px;

                  img {
                    width: 100%;
                  }
                }

                .view_x {
                  margin: 0 10px;
                  color: #ccc;
                }

                .view_text {
                  font-size: 22px;
                }
              }

              .view_input {
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                margin-top: 35px;

                .input_value {
                  position: relative;
                  border-radius: 3px;
                  // width: 280px;
                  height: 40px;
                  background-color: #f6f6f6;
                  box-sizing: border-box;

                  &:nth-child(2) {
                    margin: 20px 0;
                  }

                  &:nth-child(3) {
                    display: flex;
                  }

                  .inp {
                    height: 30px;
                    width: 280px;
                    border: 0;
                    background: transparent;
                    height: 40px;
                    font-size: 16px;
                    line-height: 21px;
                    color: #000000;
                  }

                  // .inp_code {
                  //   width: 80px;
                  // }

                  .input_auth_code {}
                }

                .button-view {
                  position: relative;
                  margin-top: 16px;
                  width: 100%;
                  display: flex;
                  align-items: center;

                  >div:nth-child(2) {
                    width: 40%;
                    padding-left: 8px;
                    box-sizing: border-box;
                  }

                  .login_btn {
                    width: 100%;
                    height: 40px;
                    border: 0px solid rgba(40, 40, 40, 0.47);
                    background: #1964d6;
                    font-size: 20px;
                  }

                  .password_btn {
                    position: absolute;
                    right: 0px;
                    bottom: -24px;
                    font-size: 14px;
                    cursor: pointer;
                  }

                  .tec_login_btn {
                    position: absolute;
                    left: 0px;
                    bottom: -24px;
                    font-size: 14px;
                    cursor: pointer;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

:deep(.el-input-group__append) {
  padding: 0;
  //   width: 120px;
  cursor: pointer;
  box-sizing: border-box;
}

.code-image {
  height: 100% !important;
  cursor: pointer;
  text-align: center;
}

.el-input {
  --el-input-bg-color: #f5f5f5;
  --el-input-border-color: #f5f5f5;
}
</style>